﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

namespace t_205_verk_hopur_12.Models
{
    public partial class Channel
    {
        // Validation methods copied from NerdDinner tutorial
        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        // ATT: Add validation for autocracy and station Type
        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(Name))
                yield return new RuleViolation("Sláðu inn nafnA", "Name");
            if (String.IsNullOrEmpty(Description))
                yield return new RuleViolation("Sláðu inn lýsingu á stöð", "Description");
            if (String.IsNullOrEmpty(Autocracy.ToString()))
                yield return new RuleViolation("Veldu lýðræði eða einræði", "Autocracy");
            if (String.IsNullOrEmpty(CategoryID.ToString()))
                yield return new RuleViolation("Skráðu flokk á stöð", "Category");

            yield break;
        }

        // Catches errors from validation
        public class RuleViolation
        {
            // Properites
            public string ErrorMessage { get; private set; }
            public string PropertyName { get; private set; }

            // Constructor
            public RuleViolation(string errorMessage)
            {
                ErrorMessage = errorMessage;
            }

            // Constructor
            public RuleViolation(string errorMessage, string propertyName)
            {
                ErrorMessage = errorMessage;
                PropertyName = propertyName;
            }
        }

        partial void OnValidate(ChangeAction action)
        {
            if (!IsValid)
            {
                throw new ApplicationException("Rule violations prevent saving");
            }
        }
    }
}
